$(function() {
	//系统管理
	//----码表管理
	/**
	 * Init
	 *
	 * 
	 * |-- 
	 * |   `-- 
	 * |   `-- 
	 * |	   `-- 
	 * |		   `-- 
	 * |		   `-- 
	 * |			   `-- 
	 * |		   `-- 
	 * |-- 
	 * |-- 
	 */
	var ad = appData();
	var pId=null

	loadDom()

	function loadDom() {

		showTable(true, 1)

		loadCase()

	}

	function loadCase() {
		$("#backShow").on('click', function() {

			showTable(true, 1)

		})
		$("#addbtn").on('click', function() {
			var name = $(this).text()
			//$("#tableBody input").length?'':return false;
			if(!$("#tableBody input").length) {
				if(name == '添加编码') {
					$("#tableBody").prepend(`<tr><td></td><td><input type='text'  saveVal_1='save' placeholder='请输入编码类型名称'></td>
					<td><input type='text'  saveVal_2='save' placeholder='请输入编码类型名称'></td>
					<td class='icons'>
					<a href='javascript:void(0)' saveId='saveBtn'  title='保存'><i class='icon-save'></i></a>
					<a href="javascript:void(0)" delIds='delBtn' title="删除"><i class="icon-delete"></i></a>
					</td></tr>`)

				} else if(name == '添加编码类型') {
					$("#tableBody").prepend(`<tr><td></td><td><input type='text'  id='typename' placeholder='请输入编码类型名称'></td>
					<td class='icons'>
					<a href='javascript:void(0)' saveId='saveBtn'  title='保存'><i class='icon-save'></i></a>
					<a href="javascript:void(0)" delId='delBtn' title="删除"><i class="icon-delete"></i></a>
					</td></tr>`)

					
				}
				$("#tableBody a[saveId]").on('click', function() {

						
						var api = ['CODE_ADD_TYPE_P','CODE_ADD_TYPE_C']
						if(val == '') {
							_as.jAlert('输入值不能为空')
							return
						}
						if(name == '添加编码'){
							var val = $(this).parents('tr').find('input').eq(0).val()
							var code = $(this).parents('tr').find('input').eq(1).val()
							var param = {
								valueName: val,
								valCode:code,
								id:pId
							}
							ad.insert(api[1], param, function() {
                                showTable_2(true, 1,pId)
						})

						}else if(name=='添加编码类型'){
							var val = $(this).parents('tr').find('input').val()
							if(val.length>30){
                                _as.jAlert('不能超过30个字符')
                                $(this).parents('tr').find('input').val('')
                                return
							}
							var param = {
								valueName: val
							}
							ad.insert(api[0], param, function() {
                                showTable(true, 1)
						})

						}
						

						
					})
					$("#tableBody a[delId]").on('click', function() {

						$(this).parents('tr').remove()
					})

			}

		})
		$("#search").on('click', function() {
			if($("#addbtn").text()=='添加编码'){
				showTable_2(true,1,pId)
			}else{
				showTable(true, 1)
			}
			
		})
	}

	function showTable(dontRenderPager, page) {
		$("#thShow").hide()
		$("#backShow").hide()
		$("#titleName").text('码表类型')
		$("#addbtn").text('添加编码类型')
		var params = {
			"page": page,
			"keyValue": $("#keyVal").val(),
			"pageType": '',
			"pageSize": appApi('PAGE_SIZE')
		}

		ad.selectList("CODE_LIST_PARENT", params, function(data) {

			$("#tableBody").empty();
			if(data.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}

			for(var i = 0; i < data.content.length; i++) {
				$("#tableBody").append(`<tr data-id="${data.content[i].id}">
              <td>${i+1}</td>
              <td>${data.content[i].name}</td>
              <td class="icons">
              <a href="javascript:void(0);" edit='edit'  title="修改"><i class="icon-setting"></i></a>
              <a href="javascript:void(0);" del='del' title="删除"><i class="icon-delete"></i></a>
              <a href="javascript:void(0)" search='search' title="子节点"><i class="icon-tree"></i></a>
              </td>
            </tr>`)
			}
			if(dontRenderPager) {
				renderPager(data.totalRecord);
			}

			eachCase()
			//单条删除
			//              delOnly()
			//              //单条状态编辑
			//              editOnly()

		})
	}

	function renderPager(totalRecord, nowPage) {

		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {

			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
			}
		}, nowPage);
	};

	function eachCase() {
		//设置
		var p = true
		$("#tableBody a[edit]").on('click', function() {
			if(p){
				var id = $(this).parents('tr').attr('data-id')
				var dom = $(this).parents('tr').find('td').eq(1)
				var val = dom.text()
				dom.html(`<input type="text"  value=${val}>`)
				p=false
				dom.find('input').on('blur', function() {
					val = $(this).val()
					dom.html(`${val}`)
					p=true
					var param = {
						id: id,
						value: val
					}
					ad.insert('CODE_EIDT_TYPE_P', param, () => (showTable(true, 1)))
				})
			}
			
		})
		//删除
		$("#tableBody a[del]").on('click', function() {
			let name = $(this).parents('tr').find('td').eq(1).text()
			var param = {
				id: $(this).parents('tr').attr('data-id')
			}
		
			_as.jConfirm('确定是否要删除'+name+'项？',function(){
				
				ad.del('CODE_DEL_TYPE_P', param, () => (showTable(true, 1)))
			})
			
			

		})
		//查看
		$("#tableBody a[search]").on('click', function() {
			var id = $(this).parents('tr').attr('data-id')
			var name = $(this).parents('tr').find('td').eq(1).text()
		    pId=id
			$("#titleName").text('码表：' + name)
			$("#addbtn").text('添加编码')

			showTable_2(true, 1, id)

		})

	}

	function showTable_2(dontRenderPager, page, id) {
		$("#thShow").show()
		$("#backShow").show()

		var params = {
			"page": page,
			"keyValue": $("#keyVal").val(),
			"id": id?id:'',
			"pageSize": appApi('PAGE_SIZE')
		}

		ad.selectList("CODE_LIST_CHILD", params, function(data) {

			$("#tableBody").empty();
			if(data.content.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}

			for(var i = 0; i < data.content.length; i++) {
				$("#tableBody").append(`<tr data-id="${data.content[i].id}">
              <td>${i+1}</td>
              <td>${data.content[i].name}</td>
              <td>${data.content[i].code}</td>
              <td class="icons">
              <a href="javascript:void(0);" del_c='del' title="删除"><i class="icon-delete"></i></a>
              </td>
            </tr>`)
			}
			//删除
		$("#tableBody a[del_c]").on('click', function() {
			var param = {
				id: $(this).parents('tr').attr('data-id')
			}
			ad.del('CODE_DEL_TYPE_C', param, () => (showTable_2(true, 1,pId)))

		})
			if(dontRenderPager) {
				renderPager_2(data.totalRecord,null,id);
			}

		})
	}

	function renderPager_2(totalRecord, nowPage,id) {

		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {

			callback: function(page_id, jq) {
				pageNumNow2 = page_id + 1;
				showTable_2(false, pageNumNow2,id);
			}
		}, nowPage);
	};

})